package com.didibigdata.servlets;

import com.didibigdata.entities.Trj;
import com.didibigdata.utils.GPSUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/TrackServlet")
public class TrackServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultSet[] resultSetmysql = new ResultSet[3];
        Connection connectionmysql = null;
        PreparedStatement[] statementmysql = new PreparedStatement[3];
        //定义SQL名
        String[] sqlName = new String[3];
        for (int i = 0; i < 3; i++) {
            int ii = i + 1;
            if (i < 9){
                sqlName[i] = "t_gpstestdel0" + ii + "";
            }else {
                sqlName[i] = "t_gpstestdel" + ii + "";
            }
        }
        String[] sqltime = new String[3];
        List<Trj> trjList = new ArrayList<Trj>();
        //trjList中，id 用来放 第几条订单
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connectionmysql = DriverManager.getConnection("jdbc:mysql://localhost:3306/didi", "root", "123456");
            for (int i = 0; i < 3; i++) {
                //从数据库读数据到三条resultSetmysql[i]上
                sqltime[i] = "select * from " + sqlName[i] + "";
                statementmysql[i] = connectionmysql.prepareStatement(sqltime[i]);
                resultSetmysql[i] = statementmysql[i].executeQuery();
                System.out.println(sqltime[i]);
                //将resultSetmysql[i]数据写入List
                int RelativeLongitude = 0;
                int RelativeLatitude = 0;
                int GB0 = 0;
                int GB1 = 0;
                while (resultSetmysql[i].next()) {
                    Trj  trj = new Trj();
                    GPSUtil gps = new GPSUtil();
                    double[] GtoB = new double[2];
                    GtoB = gps.gcj02_To_Bd09(Double.valueOf(resultSetmysql[i].getString(3)),Double.valueOf(resultSetmysql[i].getString(4)));
                    // System.out.println(GtoB[0]);
                    RelativeLongitude = (int)(GtoB[0] * 10000) - GB0;
                    RelativeLatitude =  (int)(GtoB[1] * 10000) - GB1;
                    trj.setId(i);
                    trj.setLongitude(RelativeLongitude);
                    trj.setLatitude(RelativeLatitude);
                    trjList.add(trj);
                    GB0 = (int)(GtoB[0] * 10000);
                    GB1 = (int)(GtoB[1] * 10000);
                }
                statementmysql[i].close();
            }
            connectionmysql.close();
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(trjList);
            response.setContentType("text/html; charset=utf-8");
            response.getWriter().write(json);
            System.out.println(json);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                for (int i = 0; i < 3; i++) {
                    statementmysql[i].close();
                }
                connectionmysql.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
